﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class CalComps : MonoBehaviour
{
    private List<int[]> history;

    private void Awake()
    {
        history = reloadShuangseQiu();
    }


    public const string fileName = "./ball_history.txt";

    public const string shafileName = "./ball_sha.txt";

    public List<List<int>> loadFileData()
    {
        var envCurData = new List<List<int>>();
        var allLines = File.ReadAllLines(fileName);
        for (int i = 0; i < allLines.Length; i++)
        {
            string line = allLines[i];
            var result = line.Split(',');
            List<int> rest_int = new List<int>();
            for (int j = 0; j < result.Length; j++)
            {
//                Debug.Log(result[i]);
                var ret = int.Parse(result[j]);
                rest_int.Add(ret);
            }

            envCurData.Add(rest_int);
        }

        return envCurData;
    }

    public string[] loadHistoryData()
    {
        var allLines = File.ReadAllLines(shafileName);

        return allLines;
    }

    private StreamWriter _writer;
    List<List<int>> cacheSaveData = new List<List<int>>();
    private Action saveFunc = null;
    private Action<List<int>> updateFunc = null;

    public void SaveData(List<List<int>> data, Action<List<int>> updateFunc, Action successFunc)
    {
        cacheSaveData.Clear();
        cacheSaveData.AddRange(data);
        if (_writer != null)
        {
            _writer.Dispose();
        }

        this.updateFunc = updateFunc;
        saveFunc = successFunc;
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }

        _writer = File.AppendText(fileName);
    }

    private void Update()
    {
        if (cacheSaveData.Count > 0)
        {
            for (int i = 0; i < 1000; i++)
            {
                var firstData = cacheSaveData[0];
                cacheSaveData.RemoveAt(0);
                _writer.Write(
                    $"{firstData[0]},{firstData[1]},{firstData[2]},{firstData[3]},{firstData[4]},{firstData[5]}\n");

                this.updateFunc?.Invoke(firstData);
                if (cacheSaveData.Count == 0)
                {
                    if (saveFunc != null)
                    {
                        saveFunc();
                    }

                    _writer.Dispose();
                    _writer = null;
                    return;
                }
            }
        }
    }

    public List<int[]> reloadShuangseQiu()
    {
        List<int[]> datas = new List<int[]>();
        int max = 33;
        for (int i = 1; i <= max - 5; i++)
        {
            for (int i2 = i + 1; i2 <= max - 4; i2++)
            {
                for (int i3 = i2 + 1; i3 <= max - 3; i3++)
                {
                    for (int i4 = i3 + 1; i4 <= max - 2; i4++)
                    {
                        for (int i5 = i4 + 1; i5 <= max - 1; i5++)
                        {
                            for (int i6 = i5 + 1; i6 <= max; i6++)
                            {
                                datas.Add(new int[6] {i, i2, i3, i4, i5, i6});
//                                stream.WriteLine($"{i} , {i2} , {i3} , {i4} , {i5} , {i6}");
                            }
                        }
                    }
                }
            }
        }

        return datas;
    }
}